package com.example.yjhero.safeapp.activity;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import com.example.yjhero.safeapp.R;
import com.example.yjhero.safeapp.adapter.HomeAdapter;
import com.example.yjhero.safeapp.dialog.Enterpswdialog;
import com.example.yjhero.safeapp.dialog.Setpswdialog;
import com.example.yjhero.safeapp.receiver.MyDeviceAdminReciever;

public class HomeActivity extends AppCompatActivity {

    private GridView gv_home;
    private DevicePolicyManager policyManager;
    private ComponentName componentName;
    SharedPreferences mSp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        initData();
    }
    /**
     * 功能逻辑判断
     */
    private void initData() {
        // 1.获取设备管理员
        policyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        // 2.申请权限, MyDeviceAdminReciever继承自DeviceAdminReceiver
        componentName = new ComponentName(this, MyDeviceAdminReciever.class);
        // 3.判断,如果没有权限则申请权限
        boolean active = policyManager.isAdminActive(componentName);
        if (!active) {
            //没有管理员的权限，则获取管理员的权限
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "获取超级管理员权限，用于远程锁屏和清除数据");
            startActivity(intent);
        }
        mSp=getSharedPreferences("config",MODE_PRIVATE);
        gv_home.setAdapter(new HomeAdapter(this));
        gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: // 手机防盗 模块
                        // 判断是否已经输入过安全密码
                        if (isSetupPassword()) { // 如果已输入,则弹出"输入密码"对话框
                            showInterPswDialog();
                        }else {// 没有, 则弹出"两次确认密码"对话框
                            showSetupPswDialog();
                        }
                        break;
                    case 1: // 通讯卫士 模块
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    case 8:
                        break;
                }
            }
        });
    }

    /**
     * 显示"两次确认密码"对话框
     */
    private void showSetupPswDialog() {
        final Setpswdialog setUpPasswordDialog = new Setpswdialog(this);
        setUpPasswordDialog.setmMyCallBack(new Setpswdialog.MyCallBack() {
            @Override
            public void ok() {
                String firstPWD = setUpPasswordDialog.getmFirstPWDET();
                String affirmPWD = setUpPasswordDialog.getmAffirmET();
                if (!TextUtils.isEmpty(firstPWD) // 两个输入框都不能为空
                        && !TextUtils.isEmpty(affirmPWD)) {
                    // 判断两次输入一致
                    if (firstPWD.equals(affirmPWD)) {
                        // 存储到本地文件
                        savePswd(affirmPWD);
                        setUpPasswordDialog.dismiss();
                        // 显示输入密码对话框
                        showInterPswDialog();
                    } else {
                        Toast.makeText(HomeActivity.this, "两次密码不一致!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HomeActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void cancel() {
                setUpPasswordDialog.dismiss();
            }
        });
        setUpPasswordDialog.setCancelable(true);
        setUpPasswordDialog.show();
    }


/**
 * 保存密码至本地
 * */
    private void savePswd(String affirmPWD) {
        SharedPreferences.Editor editor=mSp.edit();//自己做成Util
        editor.putString("phoneAntiTheftPWD",affirmPWD);
        editor.commit();
    }
    /**
     * "输入密码"对话框
     */
    private void showInterPswDialog() {
            final Enterpswdialog enterpswdialog=new Enterpswdialog(this);
            enterpswdialog.setmMyCallBack(new Enterpswdialog.MyCallBack() {
                @Override
                public void ok() {
                    String enterpsw=enterpswdialog.getmEnterED();
                    String psw= mSp.getString("phoneAntiTheftPWD",null);
                    if(TextUtils.isEmpty(enterpsw)){
                        Toast.makeText(HomeActivity.this,"密码为空",Toast.LENGTH_SHORT).show();
                    } else if(TextUtils.equals(enterpsw,psw)){
                        Toast.makeText(HomeActivity.this,"密码正确",Toast.LENGTH_SHORT).show();
                        enterpswdialog.dismiss();
                        startActivity(LostFirstActivity.class);
                    }
                    else {
                        Toast.makeText(HomeActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void cancel() {
                    enterpswdialog.dismiss();
                }
            });
            enterpswdialog.setCancelable(true);
            enterpswdialog.show();
    }

    /**
     * 密码正确,进防盗设置界面
     * */
    private void startActivity(Class<?> cls) {
        Intent intent = new Intent(HomeActivity.this, cls);
        this.startActivity(intent);
    }

    /**
     * 判断是否已经输入过安全密码
     */
    private boolean isSetupPassword() {
        String psw= mSp.getString("phoneAntiTheftPWD",null);
        if(TextUtils.isEmpty(psw)){
            return false;
        }
        return true;
    }

    /**
     * 初始化界面
     */
    private void initView() {
        gv_home = (GridView) findViewById(R.id.gv_home);
    }
}